package com.midian.controller.advertisePic;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.midian.common.filter.annotation.Validate;
import com.midian.common.system.RetManager;
import com.midian.common.utils.ContentJSONObject;
import com.midian.model.advertisePic.AdvertisePic;
import com.midian.service.advertisePic.IAdvertisePicService;

import net.sf.json.JSONObject;

/**广告图片控制*/
@Controller(value = "advertisePicController")
public class AdvertisePicController {
	
	private static Logger LOG = Logger.getLogger(AdvertisePic.class);
	
	private IAdvertisePicService advertisePicService;
	
	@Resource(name = "advertisePicService")
	public void setAdvertisePicService(IAdvertisePicService advertisePicService) {
		this.advertisePicService = advertisePicService;
	}
	
	@Validate(retCode = RetManager.common_error)
	@RequestMapping(value="/advertisePic",method=RequestMethod.GET)
	public @ResponseBody
	Object advertisePic(String client_key,
			@RequestParam(required=false)String advertisePicId) throws Exception{
		JSONObject json = new JSONObject();
		try {
			//获取广告图片，只有1张，id取1
			if (advertisePicId == null) {
				advertisePicId="1";
			}
			AdvertisePic advertisePic = advertisePicService.get(advertisePicId);
			
			ContentJSONObject contentObject = new ContentJSONObject();
			contentObject.put("advertise_url", advertisePic.getUrl());
			contentObject.put("advertise_pic_id", advertisePic.getPicId());
			
			json.put("content", contentObject.getContent());
		} catch (Exception e) {
			LOG.error(e.getMessage());
			throw e;
		}
		RetManager.getInstance().setRetSuccess(json, RetManager.common_submit_success);
		return json;
	}
	
	
}
